@echo off
chcp 65001
setlocal enabledelayedexpansion

set buildType=%1
set branch=%2
set androidEnv=%3

set projectPath=C:\mateurp_pipline\mate-client-adnroid
set aarPath=C:\mateurp_pipline\mateurp\mateurp\Build\Android\unityLibrary.aar 
set mateAndroidAarPath=C:\mateurp_pipline\mate-client-adnroid\u3d-debug-library\unityLibrary.aar

cd /d %projectPath%

:: Update Code
echo Update Code Start
git checkout %branch%
git fetch origin || exit /b 1
git reset --hard "origin/%branch%" || exit /b 1
echo Update Code Finish

IF EXIST "C:\mateurp_pipline\mate-client-adnroid\gradle_.properties" (
    copy /Y C:\mateurp_pipline\mate-client-adnroid\gradle_.properties C:\mateurp_pipline\mate-client-adnroid\gradle.properties
) ELSE (
    echo The file gradle_.properties does not exist.
)

:: Move AAR
echo Move AAR 
copy /Y %aarPath% %mateAndroidAarPath%

:: Build APK
echo Build APK
rd /s /q C:\mateurp_pipline\mate-client-adnroid\app\build
call gradlew clean
call gradlew app:assemble%androidEnv% --refresh-dependencies || exit 1

:: Upload Symbol
::set hour=%time:~0,2%
::if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
::set substring=%date:~3,4%-%date:~8,2%-%date:~11,2%-%hour%-%time:~3,2%-%time:~6,2%
::set buildDir=C:\mateurp_pipline\mateurp\mateurp\Build\Android\RELEASE\MateAi\unityLibrary
::set symbolFileName=symbols_%substring%.zip
::"C:\Program Files\7-Zip\7z.exe" a -r "%buildDir%\symbols.zip" "%buildDir%\symbols\*"
::C:\mateurp_pipline\obsutil\obsutil cp "%buildDir%\symbols.zip" obs://wlops/mateurp_android/RELEASE/%symbolFileName%
::set symbleUrl=https://wlops.obs.cn-north-4.myhuaweicloud.com/mateurp_android/RELEASE/%symbolFileName%

::::DebugTestApk
C:\mateurp_pipline\obsutil\apk_upload_v3.bat -e %androidEnv% -t 1dd9ef311951334ba9e001f194f78c99ee8b74fff2c56b5f899c1727302f60b8 -m "上传mate-%androidEnv% apk到应用商店完成 https://appstore.wekoi.cn/#/appDetail/116" 
::&& C:\mateurp_pipline\obsutil\apk_upload_v3.bat -e Release -t 1dd9ef311951334ba9e001f194f78c99ee8b74fff2c56b5f899c1727302f60b8 -m "上传mate正式apk到应用商店完成" 

echo END Build APK

set substring=%date:~3,4%-%date:~8,2%-%date:~11,2%-%hour%-%time:~3,2%-%time:~6,2%
set buildDir=C:\mateurp_pipline\mateurp\mateurp\Build\Android\%buildType%\MateAi\unityLibrary
set symbolFileName=mate_android_symbols_%packageName%_%substring%.zip
"C:\Program Files\7-Zip\7z.exe" a -r "%buildDir%\symbols.zip" "%buildDir%\symbols\*"
move /Y "%buildDir%\symbols.zip" C:\mateurp_pipline\Mate\%symbolFileName%
set symbleUrl=http://%ip%/Mate/%symbolFileName%
echo symbleUrl: %symbleUrl%
set json="{\"text\":{\"content\":\"符号表下载地址 %symbleUrl%\" },\"msgtype\":\"text\"}"
echo %json%
curl --ssl-no-revoke -H "Content-Type: application/json" -X POST -d !json! https://oapi.dingtalk.com/robot/send?access_token=1dd9ef311951334ba9e001f194f78c99ee8b74fff2c56b5f899c1727302f60b8


endlocal